﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace class7shu
{
    public partial class Form1 : Form
    {
        private Graphics graphics;

        public int n = 10;
        //定义递归深度(默认为10）
        public double leng = 100;
        //定义初始主干长度（默认为100）
        public double per1 = 0.6;
        //定义初始右分支长度比（默认为0.6）
        public double per2 = 0.7;
        //定义初始左分支长度比（默认为0.7）
        public double th1 = 30 * Math.PI / 180
            ;//定义右分支角度（初始为三十度）
        public double th2 = 20 * Math.PI / 180;
        //定义左分支角度（初始为二十度)
        string color;
      
        public Pen DrawPen { get; set; }//画笔

        void drawCayleyTree(int n, double x0, double y0, double leng, double th)//层数，横纵坐标，长度，角度
        {
            if (n == 0) return;
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);//递归作画
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        void drawLine(double x0, double y0, double x1, double y1)
        {
            
                graphics.DrawLine(Pens.Red, (int)x0, (int)y0, (int)x1, (int)y1);//根据两点连线
            /*
            if (color == "Black")
            {
                graphics.DrawLine(Pens.Black, (int)x0, (int)y0, (int)x1, (int)y1);//根据两点连线
            }
            if (color == "Blue")
            {
                graphics.DrawLine(Pens.Blue, (int)x0, (int)y0, (int)x1, (int)y1);//根据两点连线
            }
            if (color == "Yellow")
            {
                graphics.DrawLine(Pens.Yellow, (int)x0, (int)y0, (int)x1, (int)y1);//根据两点连线
            }
            if (color == "Green")
            {
                graphics.DrawLine(Pens.Green, (int)x0, (int)y0, (int)x1, (int)y1);//根据两点连线
            }*/
        }

        public Form1()
        {
            InitializeComponent();
             
            //对组合框（选择颜色）进行初始化
           /* ColorComboBox.Items.Add("Red");
            ColorComboBox.Items.Add("Black");
            ColorComboBox.Items.Add("Blue");
            ColorComboBox.Items.Add("Yellow");
            ColorComboBox.Items.Add("Green");
            */
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
             n = trackBar1.Value;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void trackBar4_Scroll(object sender, EventArgs e)
        {
            per2 = trackBar4.Value;
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
             
            if (graphics == null) graphics = this.CreateGraphics();
            this.drawCayleyTree(this.n, 480, 320, leng, -Math.PI / 2);
        }

        private void label1_Click(object sender, EventArgs e)
        {
            this.Name = "请输入递归深度：";
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.Name = "请输入主干深度：";
        }

        private void label3_Click(object sender, EventArgs e)
        {
            this.Name = "请输入右分支长度比：";
        }

        private void label4_Click(object sender, EventArgs e)
        {
            this.Name = "请输入左分支长度比：";
        }

        private void label5_Click(object sender, EventArgs e)
        {
            this.Name = "请输入右分支角度：";
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            leng = trackBar2.Value;
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            per1 = trackBar3.Value;
        }

        private void trackBar5_Scroll(object sender, EventArgs e)
        {
            th1 = trackBar5.Value;
        }

        private void trackBar6_Scroll(object sender, EventArgs e)
        {
           th2 = trackBar6.Value;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            graphics = graphics = panel1.CreateGraphics();
            this.drawCayleyTree(this.n, 480, 320, leng, -Math.PI / 2);
        }
    }
}
